package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanUtils;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.DealTransInBean;
import com.jrx.anytxn.transaction.bean.DelqTransInSaveBean;
import com.jrx.anytxn.transaction.bean.TransferSaveBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.mapper.ext.ExtBtAccountingTransferListMapper;
import com.jrx.anytxn.transaction.service.IDealDelqService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 客户批量-转表内处理
 * @author: xiaoyu.jing
 * @date: 2020-09-01 15:06
 **/
@Order(30)
@Service
public class TransInHandle extends BaseBatchAccoutHandle<TransferSaveBean> {

    Logger logger = LoggerFactory.getLogger(TransInHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private ExtBtAccountingTransferListMapper transferListMapper;

    @Autowired
    private IDealDelqService delqService;

    /**
     * 转表内处理业务逻辑
     * @return
     * @throws TxnException
     */
    @Override
    public List<TransferSaveBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        CmCustomerFirstLevel dealCust = custBatchSaveBean.getDealCust();
        String customerId = dealCust.getCustomerId();
        String tenantId = dealCust.getTenantId();
        PrOrganizationTable org = organizationTableService.findByOrId(TransBizConstant.DEFAULT_ORG_ID,tenantId);        Date today = org.getToday();
        List<BtAccountingTransferList> transferListList = transferListMapper.selectByTransList(customerId,tenantId,today);
        if(CollectionUtils.isEmpty(transferListList)){
            return null;
        }

        List<TransferSaveBean> transferSaveBeanList = new ArrayList<>();
        List<AmLoanMainInfo> dealLoanMainList = custBatchSaveBean.getDealLoanMainList();
        List<AmLoanAccountInfo> dealLoanAcctList = custBatchSaveBean.getDealLoanAccountList();
        List<AmAccountMainInfo> dealAcctList = custBatchSaveBean.getDealAcctList();
        List<AmRepaymentPlanInfo> dealPlanList = custBatchSaveBean.getDealPlanList();
        List<AmInterestAccrualInfo> dealAccuAcctList = custBatchSaveBean.getDealAccuAcctList();
        for(BtAccountingTransferList transferList : transferListList){
            logger.info("DelqTransIn处理transferList:{}", BeanUtils.toMap(transferList));
            String loanId = transferList.getLoanId();;
            AmLoanAccountInfo loanAccountInfo = dealLoanAcctList.stream()
                    .filter(loanAcct -> loanId.equals(loanAcct.getLoanId()))
                    .findFirst().orElse(null);
            if (null == loanAccountInfo) {
                logger.info("DelqTransIn处理借据为空");
                continue;
            }

            //添加核销订单的判断 如果是核销订单不做处理
            if(TransBizConstant.YES_FLAG.equals(loanAccountInfo.getChargeoffFlag())){
                logger.info("改订单已经核销不做处理，订单号：{}",loanAccountInfo.getLoanId());
                continue;
            }
            logger.info("DelqTransIn处理loan:{}", BeanUtils.toMap(loanAccountInfo));

            AmLoanMainInfo loanMainInfo = dealLoanMainList.stream()
                    .filter(loanMain -> loanId.equals(loanMain.getLoanId()))
                    .findFirst().get();

            DealTransInBean dealTransInBean = new DealTransInBean();
            dealTransInBean.setBusinessDate(today);
            dealTransInBean.setLoanAcct(loanAccountInfo);
            dealTransInBean.setLoanMain(loanMainInfo);
            dealTransInBean.setPlanInfoList(dealPlanList);
            dealTransInBean.setAcctList(dealAcctList);
            dealTransInBean.setAccuAcctList(dealAccuAcctList);
            DelqTransInSaveBean delqTransInSaveBean = delqService.dealDelqTransIn(dealTransInBean);
            if(null != delqTransInSaveBean){
                transferSaveBeanList.addAll(delqTransInSaveBean.getTransferSaveBeanList());
            }
        }
        logger.info("TransInHandle用户:{},需要处理的订单：{}",
                custBatchSaveBean.getDealCust().getCustomerId(),
                custBatchSaveBean.getDealLoanAccountList().stream().map(AmLoanAccountInfo::getLoanId).collect(Collectors.joining(","))
        );
        return transferSaveBeanList;
    }
}
